package cn.g31010.sip.component
{
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	public class ReceiveVideo extends Video
	{
		private var nc:NetConnection;
		private var ns:NetStream;
		public var address:String;
		public var streamName:String
		public var notify:Function;
		public var autoPlay:Boolean=true;
		
		public function ReceiveVideo(streamName:String,address:String,notify:Function=null)
		{
			super();
			this.streamName=streamName;
			this.address=address;
			this.notify=notify;
		}
		
		public function connect(event:CloseEvent=null):void{
			if(isConnected()){
				close();
			}
			clear();
			if(event==null||event.detail == Alert.OK){
				NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
				nc=new NetConnection();
				nc.client=new Client();
				nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect);
				nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncerr);
				nc.connect(address);
				
			}
			
		}
		private var closed:Boolean;
		public function close():void{
			closed=true;
			if(ns!=null){
				ns.close();
			}
			if(nc!=null){
				nc.close();
			}
			clear();
		}
		
		public function resume():void{
			if(ns!=null&&isConnected()){
				ns.resume();
			}
		}
		
		public function pause():void{
			if(ns!=null&&isConnected()){
				ns.pause();
			}
		}
		
		public function play():void{
			if(ns!=null&&isConnected()){
				ns.play(streamName);
			}
		}
		
		public function isConnected():Boolean{
			if(nc==null)
				return false;
			return nc.connected;
		}
		
		internal function asyncerr(e:AsyncErrorEvent):void{
            Alert.show(e.error.message);
        }
        
        internal function checkConnect (e:NetStatusEvent):void{
			var connected:Boolean=(e.info.code=="NetConnection.Connect.Success");
			closed=false;
			
			if (connected){
				ns = new NetStream(nc);
				ns.addEventListener(NetStatusEvent.NET_STATUS,nsstatus);
				attachNetStream(ns);
				var customClient:Object = new Object();
					customClient.onMetaData = function(metadata:Object):void
				{
					}
					ns.client = customClient;
					if(notify!=null){
						notify.call();
					}
					if(autoPlay){
						play();
					}
			}else if(e.info.code=="NetConnection.Connect.Closed"){
				
			}else{
				if(isConnected()){
					connect();
				}else{
					//Alert.show(e.info.code+"\n连接失败，是否重新连接？","提示：",Alert.OK|Alert.NO,null,connect);
				}
				
			}
		}
		
		private function nsstatus(e:NetStatusEvent):void{
			var info:Object=e.info;
			//Alert.show(info.code);
	if (info.code == "NetStream.Buffer.Full") {
              trace("播放状态： "+info.code);
              trace("缓冲区已满");
//              btn_pause.shape_play._visible = true;
//              btn_pause.shape_pause._visible = false;
       } else if (info.code == "NetStream.Buffer.Empty") {
              trace("播放状态： "+info.code);
              trace("数据缓冲区为空");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Failed") {
              trace("播放状态： "+info.code);
              trace("传输中有错误发生");
              //无需反馈
       } else if (info.code == "NetStream.Pause.Notify") {
              trace("播放状态： "+info.code);
              trace("播放暂停");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Play.Failed") {
              trace("播放状态： "+info.code);
              trace("播放出错");
              //严重错误
       } else if (info.code == "NetStream.Play.PublishNotify") {
              trace("播放状态： "+info.code);
              trace("发布开始，信息已经发送到所有订阅者");
              //无需反馈
       } else if (info.code == "NetStream.Play.Reset") {
              trace("播放状态： "+info.code);
              trace("播放列表重置");
              //无需反馈
       } else if (info.code == "NetStream.Play.Start") {
              trace("播放状态： "+info.code);
              trace("播放开始");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Play.Stop") {
              trace("播放状态： "+info.code);
              trace("播放停止");
              //无需反馈
       } else if (info.code == "NetStream.Play.StreamNotFound") {
              trace("播放状态： "+info.code);
              trace("播放的实时流或文件不存在");
              //严重错误
       } else if (info.code == "NetStream.Play.UnpublishNotify") {
              trace("播放状态： "+info.code);
              trace("发布停止，信息已经发送到所有订阅者");
              //无需反馈
       } else if (info.code == "NetStream.Publish.BadName") {
              trace("播放状态： "+info.code);
              trace("发布的数据流已经被发布");
              //无需反馈
       } else if (info.code == "NetStream.Publish.Idle") {
              trace("播放状态： "+info.code);
              trace("发布者闲置过长");
              //严重错误
       } else if (info.code == "NetStream.Publish.Start") {
              trace("播放状态： "+info.code);
              trace("发布开始");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Record.Failed") {
              trace("播放状态： "+info.code);
              trace("录制文件出错");
              //严重错误
       } else if (info.code == "NetStream.Record.NoAccess") {
              trace("播放状态： "+info.code);
              trace("没有足够的文件访问权限");
              //严重错误
       } else if (info.code == "NetStream.Record.Start") {
              trace("播放状态： "+info.code);
              trace("文件录制开始");
              //无需反馈
       } else if (info.code == "NetStream.Record.Stop") {
              trace("播放状态： "+info.code);
              trace("录制暂停");
              //无需反馈
       } else if (info.code == "NetStream.Seek.Failed") {
              trace("播放状态： "+info.code);
              trace("数据流定位出错");
              //严重错误
       } else if (info.code == "NetStream.Seek.Notify") {
              trace("播放状态： "+info.code);
              trace("数据搜索定位成功");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Unpause.Notify") {
              trace("播放状态： "+info.code);
              trace("恢复播放");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Unpublish.Success") {
              trace("播放状态： "+info.code);
              trace("停止发布");
              //无需反馈
       }
		}

	}
}